Let’s go over everything that is wrong with this plot from Newsweek.
It’s a duck! This image was made to look visually pleasing but it is very difficult to see the data that the image is suppose to display.
The gridlines are subtlw which is nice but there is too much wasted ink.
Where is the x axis? They don’t have one, instead they use boats to show relative sizes for bankruptcies. This could be replacedd with an x-axis an then there would be no need for any boats.
The graph is reduant. Why bother showing the boat sizes when you are also going to put the data value in as well.
Too busy with colour. The sector could be convenyed in another manner that is not a boat!
Start by loading the ggplot package.
library(ggplot2)#does this also contain the scales package?
For this task we will be working with the diamonds data set that is a default data set in ggplot. Let’s start by previewing our data.
Question: Determine how many rows the diamonds data set that comes loaded with ggplot2 has.
There are multiple ways to do this. This data frame has 53940 rows and 10 variables.You can also do nrow().
ggplot2::diamonds #this shows first 10 rows and displays how many more rows there are
## # A tibble: 53,940 × 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
## 7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47
## 8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53
## 9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49
## 10 0.23 Very Good H VS1 59.4 61 338 4.00 4.05 2.39
## # ... with 53,930 more rows
?diamonds #provides an overview of the entire data set
## starting httpd help server ...
## done
Question: Use the following code to create a reproducible subset of diamonds. Explain each line of the code in words.
set.seed(1410) #Random number generator, this is so that the the hundred rows selcted below will be random
dsmall <- diamonds[sample(nrow(diamonds), 100), ] #subset the diamond data frame, choose 100 random rows, and include all the columns, call this subset dsmall
head(dsmall) #preview the new subset of the diamond data frame
## # A tibble: 6 × 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 1.35 Ideal J VS2 61.4 57 5862 7.10 7.13 4.37
## 2 0.30 Good G VVS1 64.0 57 678 4.23 4.27 2.72
## 3 0.75 Ideal F SI2 59.2 60 2248 5.87 5.92 3.49
## 4 0.26 Ideal F VS1 60.9 57 580 4.13 4.11 2.51
## 5 0.33 Premium H VVS1 61.4 59 752 4.42 4.44 2.72
## 6 1.52 Ideal G VVS1 62.4 55 15959 7.30 7.39 4.58
Question: Use dsmall to create the following plots:
a) A scatterplot of y vs x, colored by z values and faceted by cut.
plot1<-ggplot(data=dsmall)+
geom_point(mapping=aes(x=x, y=y, color=z))+
facet_wrap(~cut, nrow=2)
#start by defining what data set to pull the information from (data=dsmall)
#specify what type of plot to make, here we want a scatter plot (geom_point)
#define what you want to plot (x=x, y=y), and within the scatter plot you want the points to by coloured by z values this goes inside the aesthic mapping, colour=z
#want to have have multiple plots faceted by cut, subdivide it by 2 rows (facet_wrap(~cut, nrow=2) )
print(plot1)
b) A scatterplot of price vs carat, colored by cut and smoothed (using the “lm” method, without standard error bars)
plot2<-ggplot(dsmall, aes(carat, price, colour = cut)) +
geom_point() +
geom_smooth(se = FALSE, method = "lm")
#start by defining what data set to pull the information from (data=dsmall)
#specify what type of plot to make, here we want a scatter plot (geom_point)
#define what you want to plot (x=carat, y=price), and within the scatter plot you want the points to by coloured by cut values this goes inside the aesthic mapping, colour=cut
#want to smooth by lm method(geom_smooth(method = "lm"))
#Don't want to see error around smoothing, se is for display confidence interval around smooth, set se to false
print(plot2)
c) A density plot of carat, faceted and colored by clarity
plot3<-ggplot(dsmall, aes(carat, color=clarity)) +
geom_density()+ facet_wrap(~clarity, nrow=3)
#start by defining what data set to pull the information from (data=dsmall)
#specify what type of plot to make, here we want a density plot (geom_density)
#define what you want to plot (aes=carat), and within the plot you want the points to by coloured by clarity values this goes inside the aesthic mapping, colour=clarity
#want to have have multiple plots faceted by clarity, subdivide it by 3 rows (facet_wrap(~clarity, nrow=2) )
print(plot3)
d) A boxplot of price as a function of cut
plot4<-ggplot(data=dsmall,mapping=aes(x=cut, y=price))+geom_boxplot()
#start by defining what data set to pull the information from (data=dsmall)
#specify what type of plot to make, here we want a box plot (geom_boxplot)
#define what you want to plot (aes(x=cut, y=price)
print(plot4)
e) A scatterplot of y versus x. The points should be red (colour = “red”), the color of the smoothing line should be blue (colour = “blue”), and the line should be dashed with fat dashes (linetype=2). The x and y labels should be set manually as well.
p <- ggplot(dsmall, aes(x, y))
plot5<-p+geom_point(color="red")+xlab("x, in mm")+ylab("y, in mm")+geom_smooth(colour="blue", linetype=2)
#start by defining what data set to pull the information from (data=dsmall)
#define what you want to plot (x=x, y=y), want these points in red
#specify what type of plot to make, here we want a scatter plot (geom_point)
#add in the x and y labels using xlab and ylab
#Add in smoothing line with geom_smooth, set colour as blue and make the line dashed (linetype=2)
print(plot5)
## `geom_smooth()` using method = 'loess'
There is a spurious correlation between the number of people who drowned by falling into a swimming pool and the number of films that Nicolas Cage appeared in. It is clear that this monster needs to be stopped. Data from this website: http://www.tylervigen.com/view_correlation?id=359
Start by making a basic scatter plot and then make it ugly. We will make the graph misleading by using two y-axes. This is the tutorial to do a dual axis plot with gg plot. http://drawar.github.io/posts/dual-y-axis-ggplot2/ To make life easier for me. I manipulated the data in the spreadsheet before plotting it.
Load in the data.
nic<-read.csv("nic.csv") #read in the data
ggplot(nic, aes(year, death, color=type)) + #define what to plot
geom_point() + geom_smooth(se = FALSE)+ #set up both scatter and line plot
xlab("Year")+ # X axis label
scale_y_continuous(
"Swimming Deaths (US)", limits = c(25, 150),
sec.axis = sec_axis(~ . * .035, name = "Nicolas Cage Films/year")) + #add in second y-axis
ggtitle("Nicolas Cage causes people to drown themselves")+ #add title
theme(plot.title = element_text(lineheight=.8, face="bold")) #modify title font
## `geom_smooth()` using method = 'loess'
Great! Now let’s add in a few fun deatils. Let’s change the scatter point dots to images. Skulls for death and Nicolas Cage face for films. Following this tutorial to add in custom images. http://stackoverflow.com/questions/27637455/display-custom-image-as-geom-point
We need to add in the image package and load in the two images.
library(gridGraphics)
## Loading required package: grid
skull <- png::readPNG("skull.png") #load in skull image
nic2 <- png::readPNG("nic2.png") #load in Nicolas Cage image
g1 <- rasterGrob(skull, interpolate=FALSE)
g2 <- rasterGrob(nic2, interpolate=FALSE)
We will use annotation custom and place a picture of either the skull or Nick Cage over the appropraiate data point.
Modify the original plot to remove the scatter points.
p<-ggplot(nic, aes(year, death, color=type)) + #define what to plot
geom_smooth(se = FALSE)+ #set up both scatter and line plot
xlab("Year")+ # X axis label
scale_y_continuous(
"Swimming Deaths (US)", limits = c(25, 150),
sec.axis = sec_axis(~ . * .035, name = "Nicolas Cage Films/year")) + #add in second y-axis
ggtitle("Nicolas Cage causes people to drown themselves")+ #add title
theme(plot.title = element_text(lineheight=.8, face="bold")) #modify title font
Define all the locations for the skulls (g1) and for the Nick Cage face (g2)
p +annotation_custom(g2, xmin=1998.5, xmax=1999.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=1999.5, xmax=2000.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=2000.5, xmax=2001.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=2001.5, xmax=2002.5, ymin=82, ymax=90)+
annotation_custom(g2, xmin=2002.5, xmax=2003.5, ymin=25, ymax=33)+
annotation_custom(g2, xmin=2003.5, xmax=2004.5, ymin=25, ymax=33)+
annotation_custom(g2, xmin=2004.5, xmax=2005.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=2005.5, xmax=2006.5, ymin=82, ymax=90)+
annotation_custom(g2, xmin=2006.5, xmax=2007.5, ymin=110, ymax=118)+
annotation_custom(g2, xmin=2007.5, xmax=2008.5, ymin=25, ymax=33)+
annotation_custom(g2, xmin=2008.5, xmax=2009.5, ymin=111, ymax=119)+
annotation_custom(g1, xmin=1998.5, xmax=1999.5, ymin=105, ymax=113)+
annotation_custom(g1, xmin=1999.5, xmax=2000.5, ymin=98, ymax=106)+
annotation_custom(g1, xmin=2000.5, xmax=2001.5, ymin=98, ymax=106)+
annotation_custom(g1, xmin=2001.5, xmax=2002.5, ymin=116, ymax=124)+
annotation_custom(g1, xmin=2002.5, xmax=2003.5, ymin=91, ymax=99)+
annotation_custom(g1, xmin=2003.5, xmax=2004.5, ymin=81, ymax=89)+
annotation_custom(g1, xmin=2004.5, xmax=2005.5, ymin=93, ymax=101)+
annotation_custom(g1, xmin=2005.5, xmax=2006.5, ymin=119, ymax=127)+
annotation_custom(g1, xmin=2006.5, xmax=2007.5, ymin=136, ymax=144)+
annotation_custom(g1, xmin=2007.5, xmax=2008.5, ymin=90, ymax=98)+
annotation_custom(g1, xmin=2008.5, xmax=2009.5, ymin=141, ymax=149)
## `geom_smooth()` using method = 'loess'
This can be uglier! Let’s add a background image. Use this titorial: https://www.r-bloggers.com/how-to-add-a-background-image-to-ggplot2-graphs/
Read in the background
mer <- png::readPNG("merman.png") #load in Nicolas Cage image
Below is the code you will use to add in the background image:
annotation_custom(rasterGrob(mer,
width = unit(1,"npc"),
height = unit(1,"npc")),
-Inf, Inf, -Inf, Inf)
Let’s Plot!
ggplot(nic, aes(year, death, color=type)) + #define what to plot
annotation_custom(rasterGrob(mer,
width = unit(1,"npc"),
height = unit(1,"npc")),
-Inf, Inf, -Inf, Inf) +
geom_smooth(se = FALSE)+ #set up both scatter and line plot
xlab("Year")+ # X axis label
scale_y_continuous(
"Swimming Deaths (US)", limits = c(25, 150),
sec.axis = sec_axis(~ . * .035, name = "Nicolas Cage Films/year")) + #add in second y-axis
ggtitle("Proof that Nicolas Cage is a Kelpie!")+ #add title
theme(plot.title = element_text(lineheight=.8, face="bold"))+ #modify title font
annotation_custom(g2, xmin=1998.5, xmax=1999.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=1999.5, xmax=2000.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=2000.5, xmax=2001.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=2001.5, xmax=2002.5, ymin=82, ymax=90)+
annotation_custom(g2, xmin=2002.5, xmax=2003.5, ymin=25, ymax=33)+
annotation_custom(g2, xmin=2003.5, xmax=2004.5, ymin=25, ymax=33)+
annotation_custom(g2, xmin=2004.5, xmax=2005.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=2005.5, xmax=2006.5, ymin=82, ymax=90)+
annotation_custom(g2, xmin=2006.5, xmax=2007.5, ymin=110, ymax=118)+
annotation_custom(g2, xmin=2007.5, xmax=2008.5, ymin=25, ymax=33)+
annotation_custom(g2, xmin=2008.5, xmax=2009.5, ymin=111, ymax=119)+
annotation_custom(g1, xmin=1998.5, xmax=1999.5, ymin=105, ymax=113)+
annotation_custom(g1, xmin=1999.5, xmax=2000.5, ymin=98, ymax=106)+
annotation_custom(g1, xmin=2000.5, xmax=2001.5, ymin=98, ymax=106)+
annotation_custom(g1, xmin=2001.5, xmax=2002.5, ymin=116, ymax=124)+
annotation_custom(g1, xmin=2002.5, xmax=2003.5, ymin=91, ymax=99)+
annotation_custom(g1, xmin=2003.5, xmax=2004.5, ymin=81, ymax=89)+
annotation_custom(g1, xmin=2004.5, xmax=2005.5, ymin=93, ymax=101)+
annotation_custom(g1, xmin=2005.5, xmax=2006.5, ymin=119, ymax=127)+
annotation_custom(g1, xmin=2006.5, xmax=2007.5, ymin=136, ymax=144)+
annotation_custom(g1, xmin=2007.5, xmax=2008.5, ymin=90, ymax=98)+
annotation_custom(g1, xmin=2008.5, xmax=2009.5, ymin=141, ymax=149)
## `geom_smooth()` using method = 'loess'
This is great, but can we make it uglier?! Let’s change the font type to comic sans. Tutorial: http://stackoverflow.com/questions/34522732/changing-fonts-in-ggplot2
Start by loading the font library.
library(extrafont)
## Registering fonts with R
loadfonts(device = "win")
## Agency FB already registered with windowsFonts().
## Algerian already registered with windowsFonts().
## Arial Black already registered with windowsFonts().
## Arial already registered with windowsFonts().
## Arial Narrow already registered with windowsFonts().
## Arial Rounded MT Bold already registered with windowsFonts().
## Baskerville Old Face already registered with windowsFonts().
## Bauhaus 93 already registered with windowsFonts().
## Bell MT already registered with windowsFonts().
## Berlin Sans FB already registered with windowsFonts().
## Berlin Sans FB Demi already registered with windowsFonts().
## Bernard MT Condensed already registered with windowsFonts().
## Blackadder ITC already registered with windowsFonts().
## Bodoni MT already registered with windowsFonts().
## Bodoni MT Black already registered with windowsFonts().
## Bodoni MT Condensed already registered with windowsFonts().
## Bodoni MT Poster Compressed already registered with windowsFonts().
## Book Antiqua already registered with windowsFonts().
## Bookman Old Style already registered with windowsFonts().
## Bookshelf Symbol 7 already registered with windowsFonts().
## Bradley Hand ITC already registered with windowsFonts().
## Britannic Bold already registered with windowsFonts().
## Broadway already registered with windowsFonts().
## Brush Script MT already registered with windowsFonts().
## Calibri already registered with windowsFonts().
## Calibri Light already registered with windowsFonts().
## Californian FB already registered with windowsFonts().
## Calisto MT already registered with windowsFonts().
## Cambria already registered with windowsFonts().
## Candara already registered with windowsFonts().
## Castellar already registered with windowsFonts().
## Centaur already registered with windowsFonts().
## Century already registered with windowsFonts().
## Century Gothic already registered with windowsFonts().
## Century Schoolbook already registered with windowsFonts().
## Chiller already registered with windowsFonts().
## Colonna MT already registered with windowsFonts().
## Comic Sans MS already registered with windowsFonts().
## Consolas already registered with windowsFonts().
## Constantia already registered with windowsFonts().
## Cooper Black already registered with windowsFonts().
## Copperplate Gothic Bold already registered with windowsFonts().
## Copperplate Gothic Light already registered with windowsFonts().
## Corbel already registered with windowsFonts().
## Courier New already registered with windowsFonts().
## Curlz MT already registered with windowsFonts().
## Ebrima already registered with windowsFonts().
## Edwardian Script ITC already registered with windowsFonts().
## Elephant already registered with windowsFonts().
## Engravers MT already registered with windowsFonts().
## Eras Bold ITC already registered with windowsFonts().
## Eras Demi ITC already registered with windowsFonts().
## Eras Light ITC already registered with windowsFonts().
## Eras Medium ITC already registered with windowsFonts().
## Felix Titling already registered with windowsFonts().
## Footlight MT Light already registered with windowsFonts().
## Forte already registered with windowsFonts().
## Franklin Gothic Book already registered with windowsFonts().
## Franklin Gothic Demi already registered with windowsFonts().
## Franklin Gothic Demi Cond already registered with windowsFonts().
## Franklin Gothic Heavy already registered with windowsFonts().
## Franklin Gothic Medium already registered with windowsFonts().
## Franklin Gothic Medium Cond already registered with windowsFonts().
## Freestyle Script already registered with windowsFonts().
## French Script MT already registered with windowsFonts().
## Gabriola already registered with windowsFonts().
## Gadugi already registered with windowsFonts().
## Garamond already registered with windowsFonts().
## Georgia already registered with windowsFonts().
## Gigi already registered with windowsFonts().
## Gill Sans Ultra Bold already registered with windowsFonts().
## Gill Sans Ultra Bold Condensed already registered with windowsFonts().
## Gill Sans MT already registered with windowsFonts().
## Gill Sans MT Condensed already registered with windowsFonts().
## Gill Sans MT Ext Condensed Bold already registered with windowsFonts().
## Gloucester MT Extra Condensed already registered with windowsFonts().
## Goudy Old Style already registered with windowsFonts().
## Goudy Stout already registered with windowsFonts().
## Haettenschweiler already registered with windowsFonts().
## Harlow Solid Italic already registered with windowsFonts().
## Harrington already registered with windowsFonts().
## High Tower Text already registered with windowsFonts().
## Impact already registered with windowsFonts().
## Imprint MT Shadow already registered with windowsFonts().
## Informal Roman already registered with windowsFonts().
## Javanese Text already registered with windowsFonts().
## Jokerman already registered with windowsFonts().
## Juice ITC already registered with windowsFonts().
## Kristen ITC already registered with windowsFonts().
## Kunstler Script already registered with windowsFonts().
## Wide Latin already registered with windowsFonts().
## Leelawadee UI already registered with windowsFonts().
## Leelawadee UI Semilight already registered with windowsFonts().
## Lucida Bright already registered with windowsFonts().
## Lucida Calligraphy already registered with windowsFonts().
## Lucida Console already registered with windowsFonts().
## Lucida Fax already registered with windowsFonts().
## Lucida Handwriting already registered with windowsFonts().
## Lucida Sans already registered with windowsFonts().
## Lucida Sans Typewriter already registered with windowsFonts().
## Lucida Sans Unicode already registered with windowsFonts().
## Magneto already registered with windowsFonts().
## Maiandra GD already registered with windowsFonts().
## Malgun Gothic already registered with windowsFonts().
## Malgun Gothic Semilight already registered with windowsFonts().
## Marlett already registered with windowsFonts().
## Matura MT Script Capitals already registered with windowsFonts().
## Microsoft Himalaya already registered with windowsFonts().
## Microsoft Yi Baiti already registered with windowsFonts().
## Microsoft New Tai Lue already registered with windowsFonts().
## Microsoft PhagsPa already registered with windowsFonts().
## Microsoft Sans Serif already registered with windowsFonts().
## Microsoft Tai Le already registered with windowsFonts().
## Mistral already registered with windowsFonts().
## Modern No. 20 already registered with windowsFonts().
## Mongolian Baiti already registered with windowsFonts().
## Monotype Corsiva already registered with windowsFonts().
## MS Outlook already registered with windowsFonts().
## MS Reference Sans Serif already registered with windowsFonts().
## MS Reference Specialty already registered with windowsFonts().
## MT Extra already registered with windowsFonts().
## MV Boli already registered with windowsFonts().
## Myanmar Text already registered with windowsFonts().
## Niagara Engraved already registered with windowsFonts().
## Niagara Solid already registered with windowsFonts().
## Nirmala UI already registered with windowsFonts().
## Nirmala UI Semilight already registered with windowsFonts().
## OCR A Extended already registered with windowsFonts().
## Old English Text MT already registered with windowsFonts().
## Onyx already registered with windowsFonts().
## Palace Script MT already registered with windowsFonts().
## Palatino Linotype already registered with windowsFonts().
## Papyrus already registered with windowsFonts().
## Parchment already registered with windowsFonts().
## Perpetua already registered with windowsFonts().
## Perpetua Titling MT already registered with windowsFonts().
## Playbill already registered with windowsFonts().
## Poor Richard already registered with windowsFonts().
## Pristina already registered with windowsFonts().
## Rage Italic already registered with windowsFonts().
## Ravie already registered with windowsFonts().
## Rockwell already registered with windowsFonts().
## Rockwell Condensed already registered with windowsFonts().
## Rockwell Extra Bold already registered with windowsFonts().
## Script MT Bold already registered with windowsFonts().
## Segoe MDL2 Assets already registered with windowsFonts().
## Segoe Print already registered with windowsFonts().
## Segoe Script already registered with windowsFonts().
## Segoe UI already registered with windowsFonts().
## Segoe UI Light already registered with windowsFonts().
## Segoe UI Semibold already registered with windowsFonts().
## Segoe UI Semilight already registered with windowsFonts().
## Segoe UI Black already registered with windowsFonts().
## Segoe UI Historic already registered with windowsFonts().
## Segoe UI Symbol already registered with windowsFonts().
## Showcard Gothic already registered with windowsFonts().
## SimSun-ExtB already registered with windowsFonts().
## Snap ITC already registered with windowsFonts().
## Stencil already registered with windowsFonts().
## Sylfaen already registered with windowsFonts().
## Symbol already registered with windowsFonts().
## Tahoma already registered with windowsFonts().
## Tempus Sans ITC already registered with windowsFonts().
## Times New Roman already registered with windowsFonts().
## Trebuchet MS already registered with windowsFonts().
## Tw Cen MT already registered with windowsFonts().
## Tw Cen MT Condensed already registered with windowsFonts().
## Tw Cen MT Condensed Extra Bold already registered with windowsFonts().
## Verdana already registered with windowsFonts().
## Viner Hand ITC already registered with windowsFonts().
## Vivaldi already registered with windowsFonts().
## Vladimir Script already registered with windowsFonts().
## Webdings already registered with windowsFonts().
## Wingdings already registered with windowsFonts().
## Wingdings 2 already registered with windowsFonts().
## Wingdings 3 already registered with windowsFonts().
#This is my first time using fonts so I had to import them first using font_import()
ggplot(nic, aes(year, death, color=type)) + #define what to plot
annotation_custom(rasterGrob(mer,
width = unit(1,"npc"),
height = unit(1,"npc")),
-Inf, Inf, -Inf, Inf) +
geom_smooth(se = FALSE)+ #set up both scatter and line plot
ggtitle("Proof that Nicolas Cage is a Kelpie!") +
xlab("Year") +
theme(text=element_text(size=20, family="Comic Sans MS"))+
scale_y_continuous(
"Swimming Deaths (US)", limits = c(25, 150),
sec.axis = sec_axis(~ . * .035, name = "Nicolas Cage Films/year")) + #add in second y-axis
annotation_custom(g2, xmin=1998.5, xmax=1999.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=1999.5, xmax=2000.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=2000.5, xmax=2001.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=2001.5, xmax=2002.5, ymin=82, ymax=90)+
annotation_custom(g2, xmin=2002.5, xmax=2003.5, ymin=25, ymax=33)+
annotation_custom(g2, xmin=2003.5, xmax=2004.5, ymin=25, ymax=33)+
annotation_custom(g2, xmin=2004.5, xmax=2005.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=2005.5, xmax=2006.5, ymin=82, ymax=90)+
annotation_custom(g2, xmin=2006.5, xmax=2007.5, ymin=110, ymax=118)+
annotation_custom(g2, xmin=2007.5, xmax=2008.5, ymin=25, ymax=33)+
annotation_custom(g2, xmin=2008.5, xmax=2009.5, ymin=111, ymax=119)+
annotation_custom(g1, xmin=1998.5, xmax=1999.5, ymin=105, ymax=113)+
annotation_custom(g1, xmin=1999.5, xmax=2000.5, ymin=98, ymax=106)+
annotation_custom(g1, xmin=2000.5, xmax=2001.5, ymin=98, ymax=106)+
annotation_custom(g1, xmin=2001.5, xmax=2002.5, ymin=116, ymax=124)+
annotation_custom(g1, xmin=2002.5, xmax=2003.5, ymin=91, ymax=99)+
annotation_custom(g1, xmin=2003.5, xmax=2004.5, ymin=81, ymax=89)+
annotation_custom(g1, xmin=2004.5, xmax=2005.5, ymin=93, ymax=101)+
annotation_custom(g1, xmin=2005.5, xmax=2006.5, ymin=119, ymax=127)+
annotation_custom(g1, xmin=2006.5, xmax=2007.5, ymin=136, ymax=144)+
annotation_custom(g1, xmin=2007.5, xmax=2008.5, ymin=90, ymax=98)+
annotation_custom(g1, xmin=2008.5, xmax=2009.5, ymin=141, ymax=149)
## `geom_smooth()` using method = 'loess'
We can do better! Let’s mess around with the font colour.
ggplot(nic, aes(year, death, color=type)) + #define what to plot
annotation_custom(rasterGrob(mer, #add in the background
width = unit(1,"npc"),
height = unit(1,"npc")),
-Inf, Inf, -Inf, Inf) +
geom_smooth(se = FALSE)+ #set up line plot
ggtitle("PROOF THAT NICOLAS CAGE IS A MURDEROUS SEA KELPIE!") + #Add in title
xlab("Year") + #label x-axis
theme( #For all the text on the plot, change the font size, colour, and font type
plot.title = element_text(color="#63d63d", size=18, face="bold.italic", family="Comic Sans MS"),
axis.title.x = element_text(color="#eaff00", size=18, face="bold",family="Comic Sans MS"),
legend.text=element_text(color="#ed872d", size=14, family="Comic Sans MS"),
axis.text.x = element_text(size=14, family="Comic Sans MS", color="#00bfff"),
axis.text.y = element_text(size=14, family="Comic Sans MS", color="#00bfff"),
axis.title.y = element_text(color="#9500ff", size=18, face="bold",family="Comic Sans MS"))+
scale_y_continuous( #label first y-axis
"Swimming Deaths (US)", limits = c(25, 150),
sec.axis = sec_axis(~ . * .035, name = "Nicolas Cage Films/Year")) + #add in second y-axis, and label it
#add in the geom_points using the images of nick cage (g2) and skulls (g1)
annotation_custom(g2, xmin=1998.5, xmax=1999.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=1999.5, xmax=2000.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=2000.5, xmax=2001.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=2001.5, xmax=2002.5, ymin=82, ymax=90)+
annotation_custom(g2, xmin=2002.5, xmax=2003.5, ymin=25, ymax=33)+
annotation_custom(g2, xmin=2003.5, xmax=2004.5, ymin=25, ymax=33)+
annotation_custom(g2, xmin=2004.5, xmax=2005.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=2005.5, xmax=2006.5, ymin=82, ymax=90)+
annotation_custom(g2, xmin=2006.5, xmax=2007.5, ymin=110, ymax=118)+
annotation_custom(g2, xmin=2007.5, xmax=2008.5, ymin=25, ymax=33)+
annotation_custom(g2, xmin=2008.5, xmax=2009.5, ymin=111, ymax=119)+
annotation_custom(g1, xmin=1998.5, xmax=1999.5, ymin=105, ymax=113)+
annotation_custom(g1, xmin=1999.5, xmax=2000.5, ymin=98, ymax=106)+
annotation_custom(g1, xmin=2000.5, xmax=2001.5, ymin=98, ymax=106)+
annotation_custom(g1, xmin=2001.5, xmax=2002.5, ymin=116, ymax=124)+
annotation_custom(g1, xmin=2002.5, xmax=2003.5, ymin=91, ymax=99)+
annotation_custom(g1, xmin=2003.5, xmax=2004.5, ymin=81, ymax=89)+
annotation_custom(g1, xmin=2004.5, xmax=2005.5, ymin=93, ymax=101)+
annotation_custom(g1, xmin=2005.5, xmax=2006.5, ymin=119, ymax=127)+
annotation_custom(g1, xmin=2006.5, xmax=2007.5, ymin=136, ymax=144)+
annotation_custom(g1, xmin=2007.5, xmax=2008.5, ymin=90, ymax=98)+
annotation_custom(g1, xmin=2008.5, xmax=2009.5, ymin=141, ymax=149)
## `geom_smooth()` using method = 'loess'
Now this plot is not only insulting to Nicolas Cage but also to your eyes! But let’s make it worse by adding in gridlines and more ticks to the scales.
ggplot(nic, aes(year, death, color=type)) + #define what to plot
annotation_custom(rasterGrob(mer, #add in the background
width = unit(1,"npc"),
height = unit(1,"npc")),
-Inf, Inf, -Inf, Inf) +
geom_smooth(se = FALSE)+ #set up line plot
ggtitle("PROOF THAT NICOLAS CAGE IS A MURDEROUS SEA KELPIE!") + #Add in title
xlab("Year") + #label x-axis
theme( #For all the text on the plot, change the font size, colour, and font type
plot.title = element_text(color="#63d63d", size=18, face="bold.italic", family="Comic Sans MS"),
axis.title.x = element_text(color="#eaff00", size=18, face="bold",family="Comic Sans MS"),
legend.text=element_text(color="#ed872d", size=14, family="Comic Sans MS"),
axis.text.x = element_text(size=14, family="Comic Sans MS", color="#00bfff"),
axis.text.y = element_text(size=14, family="Comic Sans MS", color="red"),
axis.title.y = element_text(color="#9500ff", size=18, face="bold",family="Comic Sans MS"))+
scale_y_continuous( #label first y-axis
"Swimming Deaths (US)", limits = c(25, 150),
sec.axis = sec_axis(~ . * .035, name = "Nicolas Cage Films/Year")) + #add in second y-axis, and label it
#Add vertical gridlines in manually
geom_vline(xintercept = 1999, linetype=2)+
geom_vline(xintercept=2000, linetype=2)+
geom_vline(xintercept=2001, linetype=2)+
geom_vline(xintercept=2002, linetype=2)+
geom_vline(xintercept=2003, linetype=2)+
geom_vline(xintercept=2004, linetype=2)+
geom_vline(xintercept=2005, linetype=2)+
geom_vline(xintercept=2006, linetype=2)+
geom_vline(xintercept=2007, linetype=2)+
geom_vline(xintercept=2008, linetype=2)+
geom_vline(xintercept=2009, linetype=2)+
#add horizontal gridlines for films (teal)
geom_hline(yintercept=29,colour="#1EAAC2", linetype=2)+
geom_hline(yintercept=57,colour="#1EAAC2", linetype=2)+
geom_hline(yintercept=86,colour="#1EAAC2", linetype=2)+
geom_hline(yintercept=115,colour="#1EAAC2", linetype=2)+
geom_hline(yintercept=142,colour="#1EAAC2", linetype=2)+
#add horizontal gridlines for swimming deaths (red)
geom_hline(yintercept=20,colour="red", linetype=2)+
geom_hline(yintercept=40,colour="red", linetype=2)+
geom_hline(yintercept=60,colour="red", linetype=2)+
geom_hline(yintercept=80,colour="red", linetype=2)+
geom_hline(yintercept=100,colour="red", linetype=2)+
geom_hline(yintercept=120,colour="red", linetype=2)+
geom_hline(yintercept=140,colour="red", linetype=2)+
#colour="red", linetype=2
#add in the geom_points using the images of nick cage (g2) and skulls (g1)
annotation_custom(g2, xmin=1998.5, xmax=1999.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=1999.5, xmax=2000.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=2000.5, xmax=2001.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=2001.5, xmax=2002.5, ymin=82, ymax=90)+
annotation_custom(g2, xmin=2002.5, xmax=2003.5, ymin=25, ymax=33)+
annotation_custom(g2, xmin=2003.5, xmax=2004.5, ymin=25, ymax=33)+
annotation_custom(g2, xmin=2004.5, xmax=2005.5, ymin=53, ymax=61)+
annotation_custom(g2, xmin=2005.5, xmax=2006.5, ymin=82, ymax=90)+
annotation_custom(g2, xmin=2006.5, xmax=2007.5, ymin=110, ymax=118)+
annotation_custom(g2, xmin=2007.5, xmax=2008.5, ymin=25, ymax=33)+
annotation_custom(g2, xmin=2008.5, xmax=2009.5, ymin=111, ymax=119)+
annotation_custom(g1, xmin=1998.5, xmax=1999.5, ymin=105, ymax=113)+
annotation_custom(g1, xmin=1999.5, xmax=2000.5, ymin=98, ymax=106)+
annotation_custom(g1, xmin=2000.5, xmax=2001.5, ymin=98, ymax=106)+
annotation_custom(g1, xmin=2001.5, xmax=2002.5, ymin=116, ymax=124)+
annotation_custom(g1, xmin=2002.5, xmax=2003.5, ymin=91, ymax=99)+
annotation_custom(g1, xmin=2003.5, xmax=2004.5, ymin=81, ymax=89)+
annotation_custom(g1, xmin=2004.5, xmax=2005.5, ymin=93, ymax=101)+
annotation_custom(g1, xmin=2005.5, xmax=2006.5, ymin=119, ymax=127)+
annotation_custom(g1, xmin=2006.5, xmax=2007.5, ymin=136, ymax=144)+
annotation_custom(g1, xmin=2007.5, xmax=2008.5, ymin=90, ymax=98)+
annotation_custom(g1, xmin=2008.5, xmax=2009.5, ymin=141, ymax=149)
## `geom_smooth()` using method = 'loess'
## Warning: Removed 1 rows containing missing values (geom_hline).